<template>
  <h1>员工列表练习</h1>
  <input type="text" placeholder="请输入员工姓名" v-model="emp.name">
  <input type="text" placeholder="请输入员工姓名" v-model="emp.salary">
  <input type="text" placeholder="请输入员工姓名" v-model="emp.job">
  <button @click="addEmp()">点我添加</button>
  <table>
    <tr>
      <th>员工姓名</th>
      <th>员工工资</th>
      <th>员工工作</th>
    </tr>
    <tr v-for="emp in empArr">
      <td>{{emp.name}}</td>
      <td>{{emp.salary}}</td>
      <td>{{emp.job}}</td>
    </tr>
  </table>
</template>

<script setup>
import {ref} from "vue";
//创建一个emp员工的对象
const emp = ref({name: '', salary: '', job: ''});
//创建一个空数组，存放员工数据
const empArr = ref([])
//添加员工
const addEmp = () => {
  //先每个元素是否有问题
  if (emp.value.name.trim() === '' || emp.value.salary.trim() === '' || emp.value.job.trim() === ''){
    alert('输入错误，请重新输入')
    return
  }
    //.push添加元素
    empArr.value.push(emp.value);
    //清空输入框
    emp.value = {name: '', salary: '', job: ''};
}

</script>

<style scoped>

</style>